<template>
  <div>
    <div class="box">
      <el-form :model="queryForm" ref="queryRef">
        <el-row>
          <el-col :span="6">
            <el-form-item label="学生：" prop="name">
              <el-input v-model.trim="queryForm.name" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item>
              <el-button @click="getAllStudentList(null)" :loading="searchLoading" type="primary">
                <icons name="search"></icons>
                搜索
              </el-button>
              <el-button @click="reset()">
                <icons name="reset"></icons>重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="container">
      <el-button @click="openStudent()" type="primary" style="margin-bottom: 10px">+ 新增</el-button>
      <el-table :data="tableData" v-loading="tableLoading" border style="width: 100%">
        <el-table-column prop="stu_name" label="姓名" width="180" align="center" header-align="center" />
        <el-table-column prop="stu_gender" label="性别" width="80" align="center" header-align="center" />
        <el-table-column prop="stu_age" label="年龄" width="80" align="center" header-align="center" />
        <el-table-column prop="tel" label="电话" width="250" align="center" header-align="center" />
        <el-table-column prop="curNameList" label="报名课程" width="250" align="center" header-align="center">
          <template #default="{ row }">
            <p v-for="(item, index) in JSON.parse(row.curNameList)" :key="index">{{ `${index + 1}、${item}` }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="parentTel" label="父母电话" width="250" align="center" header-align="center" />
        <el-table-column label="操作" align="center" header-align="center">
          <template #default="{ row }">
            <el-button @click="editStuInfo(row)" type="text">编辑</el-button>
            <el-button @click="deleteStudentInfo(row.stu_code)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="paging">
        <el-pagination layout="total, sizes,prev,pager,next,jumper" :page-size="queryForm.pageSize"
          :page-sizes="[10, 20, 30, 40, 50]" :total="total" :current-page.sync="queryForm.page"
          @current-change="hCurrentChange" @size-change="hSizeChange" />
      </div>
    </div>
    <addStudent :valueShow="valueShow" :stuInfo="stuRowInfo" @closeStudent="closeStudent" />
  </div>
</template>
<script lang="ts">
import { queryAllStudent, deleteStudent } from '@/api/goodsOrder/addStudent'
import addStudent from './components/addStudent.vue'
import { defineComponent, reactive, ref } from 'vue'
import useCurrentInstance from '@/utils/useCurrentInstance'
import { ElMessage, ElMessageBox } from 'element-plus'
export default defineComponent({
  setup(props) {
    const { proxy } = useCurrentInstance()
    const queryRef: any = ref(null)
    const tableLoading = ref(false)
    const searchLoading = ref(false)
    const total = ref(0)
    const queryForm = reactive({
      name: null,
      page: 1,
      pageSize: 10,
    })
    const tableData = ref(null)
    const valueShow = ref(false)
    const stuRowInfo = ref(null)

    // 分页查询
    const getAllStudentList = (page) => {
      page && (queryForm.page = page)
      searchLoading.value = true
      tableLoading.value = true
      queryAllStudent(queryForm)
        .then((res: any) => {
          total.value = res.data.total
          tableData.value = res.data.list
        })
        .finally(() => {
          searchLoading.value = false
          tableLoading.value = false
        })
    }

    const editStuInfo = (row) => {
      valueShow.value = true
      stuRowInfo.value = row
    }

    const deleteStudentInfo = (code) => {
      ElMessageBox.confirm(
        '确定删除当前学生信息?',
        '',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(() => {
          deleteStudent({ code }).then((res: any) => {
            ElMessage.success(res.message)
            getAllStudentList(null)
          })
        })
        .catch(() => { })
    }

    // 重置方法
    const reset = () => {
      queryRef.value.resetFields()
    }

    // 打开新增学生弹框
    const openStudent = () => {
      valueShow.value = true
    }

    // 关闭新增学生信息弹框
    const closeStudent = (val) => {
      valueShow.value = false
      stuRowInfo.value = null
      val && getAllStudentList(null)
    }

    const hCurrentChange = (val) => {
      queryForm.page = val
    }

    const hSizeChange = (val) => {
      queryForm.pageSize = val
    }

    /**
     * 方法调用区域
     */
    getAllStudentList(null)

    return {
      total,
      queryForm,
      queryRef,
      tableLoading,
      searchLoading,
      tableData,
      valueShow,
      stuRowInfo,
      hCurrentChange,
      hSizeChange,
      getAllStudentList,
      reset,
      closeStudent,
      openStudent,
      deleteStudentInfo,
      editStuInfo
    }
  },
  components: { addStudent },
})
</script>
<style scoped lang="scss"></style>
